import Duck from 'extensible-duck'
import produce from 'immer'
import { sortByName, sortByTime } from 'components/sortData'
import { Members as MembersNorm } from './norm'

const sortMemberByTime = (members, sortFunction) => members.sort((a, b) => sortFunction(a.createAt, b.createAt))
const initAddUserModal = { isOpen: false, selected: {}, selectedTeam: {} }
const initNewTeamModal = { isOpen: false }
const initJoinedTeamsModal = { isOpen: false }
const initEditTeamModal = { isOpen: false, team: {} }
const initEditMemberModal = { isOpen: false, member: {} }
const initConfirmDeleteMember = { isOpen: false, ids: [] }
const searchUserInput = {
  queryString: '',
  state: 'normal',
  results: [],
  focus: false,
}
export default new Duck({
  namespace: 'admin',
  store: 'users',
  types: [
    'SET_USER_NAV',
    'OPEN_ADD_USER_MODAL',
    'OPEN_NEW_TEAM_MODAL',
    'CLOSE_ADD_USER_MODAL',
    'CLOSE_NEW_TEAM_MODAL',
    'RESET_ADD_USER_MODAL',
    'RESET_NEW_TEAM_MODAL',
    'RESET_SEARCH_USER',
    'CHANGE_SEARCH_USER_QUERY_STRING',
    'RECIVE_SEARCH_USER_RESULTS',
    'CHANGE_SEARCH_USER_STATE',
    'SEARCH_USER_FOCUS',
    'ADD_USER_ADD_TO_LIST',
    'ADD_USER_REMOVE_FROM_LIST',
    'RECIVE_USERS_AND_TEAMS',
    'CHANGE_SELECTED_ROLE',
    'ADD_USER_TOOGLE_TEAM',
    'SET_DELETE_MEMBERS',
    'CANCLE_DELETE_MEMBERS',
    'FINISH_DELETE_MEMBERS',
    'RECIVE_MEMBERS',
    'CHANGE_ROLE',
    'OPEN_EDIT_TEAM_MODAL',
    'SAVE_EDIT_TEAM_MODAL',
    'CLOSE_EDIT_TEAM_MODAL',
    'FINISH_EDIT_TEAM_MODAL',
    'OPEN_EDIT_MEMBER_MODAL',
    'SAVE_EDIT_MEMBER_MODAL',
    'CLOSE_EDIT_MEMBER_MODAL',
    'FINISH_EDIT_MEMBER_MODAL',
    'TOOGLE_SELECT_TEAM',
    'TOOGLE_SELECT_MEMBER',
    'TOOGLE_SELECT_ALL_TEAM',
    'TOOGLE_SELECT_ALL_MEMBER',
    'QUERY_MEMBER_STRING',
    'CHANGE_SORT_MEMBERS_METHOD',
    'OPEN_JOIN_TEAMS_MODAL',
    'CLOSE_JOIN_TEAMS_MODAL',
  ],
  initialState: {
    addUserModal: initAddUserModal,
    joinedTeamsModal: initJoinedTeamsModal,
    newTeamModal: initNewTeamModal,
    editTeamModal: initEditTeamModal,
    editMemberModal: initEditMemberModal,
    searchUser: searchUserInput,
    deleteMemberModal: initConfirmDeleteMember,
    members: {},
    selectedTeams: {},
    selectedMembers: {},
    queryMemberStr: '',
    sortMemberMethod: 'time',
  },
  reducer: produce((draft, action, duck) => {
    switch (action.type) {
      case duck.types.OPEN_ADD_USER_MODAL:
        draft.addUserModal.isOpen = true
        break
      case duck.types.OPEN_NEW_TEAM_MODAL:
        draft.newTeamModal.isOpen = true
        break
      case duck.types.CLOSE_ADD_USER_MODAL:
        draft.addUserModal.isOpen = false
        break
      case duck.types.CLOSE_NEW_TEAM_MODAL:
        draft.newTeamModal.isOpen = false
        break
      case duck.types.RESET_ADD_USER_MODAL:
        draft.addUserModal = initAddUserModal
        break
      case duck.types.RESET_NEW_TEAM_MODAL:
        draft.newTeamModal = initNewTeamModal
        break
      case duck.types.RESET_SEARCH_USER:
        draft.searchUser = searchUserInput
        break
      case duck.types.CHANGE_SEARCH_USER_QUERY_STRING:
        draft.searchUser.queryString = action.value
        draft.searchUser.state = 'normal'
        break
      case duck.types.CHANGE_SEARCH_USER_STATE:
        draft.searchUser.state = action.state
        break
      case duck.types.RECIVE_SEARCH_USER_RESULTS:
        draft.searchUser.results = action.results
        draft.searchUser.state = 'searched'
        break
      case duck.types.SEARCH_USER_FOCUS:
        draft.searchUser.focus = action.value
        break
      case duck.types.RECIVE_USERS_AND_TEAMS:
        draft.addUserModal.members = MembersNorm(action.results.GetAllMenbers)
        break
      case duck.types.ADD_USER_ADD_TO_LIST:
        draft.searchUser.state = 'selected'
        draft.addUserModal.selected[action.user.id] = Object.assign({ role: 'member' }, action.user)
        break
      case duck.types.ADD_USER_REMOVE_FROM_LIST:
        delete draft.addUserModal.selected[action.id]
        break
      case duck.types.CHANGE_SELECTED_ROLE:
        draft.addUserModal.selected[action.id].role = action.role
        break
      case duck.types.ADD_USER_TOOGLE_TEAM:
        draft.addUserModal.selectedTeam[action.id] = !draft.addUserModal.selectedTeam[action.id]
        break
      case duck.types.SET_DELETE_MEMBERS:
        draft.deleteMemberModal.ids = action.ids
        draft.deleteMemberModal.isOpen = true
        break
      case duck.types.CANCLE_DELETE_MEMBERS:
        draft.deleteMemberModal.ids = []
        draft.deleteMemberModal.isOpen = false
        break
      case duck.types.FINISH_DELETE_MEMBERS:
        action.ids.map((id) => {
          const index = draft.members.result.indexOf(id)
          draft.members.result.splice(index, 1)
          delete draft.members.entities.member[id]
        })
        draft.deleteMemberModal.ids = []
        draft.deleteMemberModal.isOpen = false
        break
      case duck.types.RECIVE_MEMBERS:
        draft.members = MembersNorm(sortMemberByTime(action.members, sortByTime))
        break
      case duck.types.CHANGE_ROLE:
        draft.members.entities.member[action.id].role = action.role
        break
      case duck.types.CLOSE_EDIT_TEAM_MODAL:
        draft.editTeamModal.isOpen = false
        break
      case duck.types.CLOSE_EDIT_MEMBER_MODAL:
        draft.editMemberModal.isOpen = false
        break
      case duck.types.OPEN_EDIT_TEAM_MODAL:
        draft.editTeamModal.isOpen = true
        break
      case duck.types.OPEN_EDIT_MEMBER_MODAL:
        draft.editMemberModal.isOpen = true
        break
      case duck.types.TOOGLE_SELECT_TEAM:
        if (action.value === true) {
          draft.selectedTeams[action.id] = action.value
        } else {
          delete draft.selectedTeams[action.id]
        }
        break
      case duck.types.TOOGLE_SELECT_MEMBER:
        if (action.value === true) {
          draft.selectedMembers[action.id] = action.value
        } else {
          delete draft.selectedMembers[action.id]
        }
        break
      case duck.types.TOOGLE_SELECT_ALL_TEAM:
        draft.members.result.map((memberid) => {
          const member = draft.members.entities.member[memberid]
          if (member.type === 'team') {
            draft.selectedTeams[member.id] = action.value
          }
        })
        break
      case duck.types.TOOGLE_SELECT_ALL_MEMBER:
        draft.members.result.map((memberid) => {
          const member = draft.members.entities.member[memberid]
          if (member.type === 'member') {
            draft.selectedTeams[member.id] = action.value
          }
        })
        break
      case duck.types.QUERY_MEMBER_STRING:
        draft.queryMemberStr = action.query
        break
      case duck.types.CHANGE_SORT_MEMBERS_METHOD:
        draft.sortMemberMethod = action.method
        break
      case duck.types.OPEN_JOIN_TEAMS_MODAL:
        draft.joinedTeamsModal.isOpen = true
        break
      case duck.types.CLOSE_JOIN_TEAMS_MODAL:
        draft.joinedTeamsModal.isOpen = false
        break
      case duck.types.SET_USER_NAV:
        draft.nav = action.value
        break
      default:
        break
    }
  }),
  selectors: {
    addUserModal: state => state.users.addUserModal,
    joinedTeamsModal: state => state.users.joinedTeamsModal,
    newTeamModal: state => state.users.newTeamModal,
    editTeamModal: state => state.users.editTeamModal,
    editMemberModal: state => state.users.editMemberModal,
    searchUser: state => state.users.searchUser,
    selectedUsers: state => state.users.addUserModal.selected,
    deleteModal: state => state.users.deleteMemberModal,
    members: state => state.users.members,
    selectedTeams: state => state.users.selectedTeams,
    selectedMembers: state => state.users.selectedMembers,
    queryMemberStr: state => state.users.queryMemberStr,
    getUserNav: state => state.users.nav,
  },
  creators: duck => ({
    actionOpenAddUserModal: () => ({ type: duck.types.OPEN_ADD_USER_MODAL }),
    actionOpenJoinedTeamsModal: () => ({ type: duck.types.OPEN_JOIN_TEAMS_MODAL }),
    actionCloseJoinedTeamsModal: () => ({ type: duck.types.CLOSE_JOIN_TEAMS_MODAL }),
    actionOpenNewTeamModal: () => ({ type: duck.types.OPEN_NEW_TEAM_MODAL }),
    actionCloseAddUserModal: () => ({ type: duck.types.CLOSE_ADD_USER_MODAL }),
    actionCloseNewTeamModal: () => ({ type: duck.types.CLOSE_NEW_TEAM_MODAL }),
    actionResetAddUserModal: () => ({ type: duck.types.RESET_ADD_USER_MODAL }),
    actionResetNewTeamModal: () => ({ type: duck.types.RESET_NEW_TEAM_MODAL }),
    actionResetSearchUser: () => ({ type: duck.types.RESET_SEARCH_USER }),
    actionChangeSearchUserState: state => ({ type: duck.types.CHANGE_SEARCH_USER_STATE, state }),
    actionChangeSearchUserQueryString: value => ({
      type: duck.types.CHANGE_SEARCH_USER_QUERY_STRING,
      value,
    }),
    actionReciveSearchUserResults: results => ({
      type: duck.types.RECIVE_SEARCH_USER_RESULTS,
      results,
    }),
    actionSearchUserFocus: value => ({ type: duck.types.SEARCH_USER_FOCUS, value }),
    actionAddUserAddtoList: user => ({ type: duck.types.ADD_USER_ADD_TO_LIST, user }),
    actionAddUserRemoveFromList: id => ({ type: duck.types.ADD_USER_REMOVE_FROM_LIST, id }),
    actionReciveUsersAndTeams: results => ({
      type: duck.types.RECIVE_USERS_AND_TEAMS,
      results,
    }),
    actionChangeSelectedRole: (id, role) => ({ type: duck.types.CHANGE_SELECTED_ROLE, id, role }),
    actionAddUserSelectedTeamToogle: id => ({ type: duck.types.ADD_USER_TOOGLE_TEAM, id }),
    actionSetDeleteMembers: ids => ({ type: duck.types.SET_DELETE_MEMBERS, ids }),
    actionCancleDeleteMember: () => ({ type: duck.types.CANCLE_DELETE_MEMBERS }),
    actionFinishDeleteMembers: ids => ({ type: duck.types.FINISH_DELETE_MEMBERS, ids }),
    actionReciveMembers: members => ({ type: duck.types.RECIVE_MEMBERS, members }),
    actionChangeMemberRole: (id, role) => ({ type: duck.types.CHANGE_ROLE, id, role }),
    actionOpenEditTeamModal: id => ({ type: duck.types.OPEN_EDIT_TEAM_MODAL, id }),
    actionCloseEditTeamModal: () => ({ type: duck.types.CLOSE_EDIT_TEAM_MODAL }),
    actionFinishEditTeamModal: (id, team) => ({
      type: duck.types.FINISH_EDIT_TEAM_MODAL,
      id,
      team,
    }),
    actionOpenEditMemberModal: id => ({ type: duck.types.OPEN_EDIT_MEMBER_MODAL, id }),
    actionCloseEditMemberModal: () => ({ type: duck.types.CLOSE_EDIT_MEMBER_MODAL }),
    actionFinishEditMemberModal: (id, member) => ({
      type: duck.types.FINISH_EDIT_MEMBER_MODAL,
      id,
      member,
    }),
    actionToogleSelectTeam: (id, value) => ({ type: duck.types.TOOGLE_SELECT_TEAM, id, value }),
    actionToogleSelectMember: (id, value) => ({ type: duck.types.TOOGLE_SELECT_MEMBER, id, value }),
    actionToogleSelectAllTeam: value => ({ type: duck.types.TOOGLE_SELECT_ALL_TEAM, value }),
    actionToogleSelectAllMember: value => ({ type: duck.types.TOOGLE_SELECT_ALL_MEMBER, value }),
    actionChangeQueryMemberStr: query => ({ type: duck.types.QUERY_MEMBER_STRING, query }),
    actionChangeSortMethod: method => ({ type: duck.types.CHANGE_SORT_MEMBERS_METHOD, method }),
    actionSetUserMav: value => ({ type: duck.types.SET_USER_NAV, value }),
  }),
})
